<?php

class NewsTable extends Doctrine_Table
{

  public function fetchall($params = array())
  {
      $q = Doctrine_Query::create()
              ->select('*')
              ->from('News');

              if(isset($params['seasonId']) && $params['seasonId']) 
                $q->where('season_id = ?', $params['seasonId']);

              if(isset($params['objectType']) && $params['objectType']) 
                $q->where('object_type = ?', $params['objectType']);
                
              if(isset($params['objectId']) && $params['objectId']) 
                $q->where('object_id = ?', $params['objectId']);
                
              // CATERGORY_ID
              if(isset($params['categoryId']) && $params['categoryId']) 
                $q->andWhere('category_id = ?', $params['categoryId']); 
              
              // KEYWORD
              if(isset($params['keyword']) && $params['keyword'])
                $q->andWhere('title LIKE ? OR intro LIKE ? OR content LIKE ?', array('%'.$params['keyword'].'%', '%'.$params['keyword'].'%', '%'.$params['keyword'].'%'));
                
              // IS_FEATURED
              if(isset($params['is_featured']) && $params['is_featured']) $q->andWhere('is_featured = 1');
              
              // SORT
              $params['order'] = (isset($params['order']) && $params['order']) ? $params['order'] : '';
              $q->orderBy($params['order'].' created_at DESC');

              // LIMIT
              $limit = isset($params['limit']) ? $params['limit'] : 4;
              $q->limit($limit);
  
      return $q->execute();
  }  
  
  
  public function getPager($params = array(), $page=1)
  {
      $q = Doctrine_Query::create()
              ->select('*')
              ->from('News');

              if(isset($params['seasonId']) && $params['seasonId']) 
                $q->where('season_id = ?', $params['seasonId']);

              if(isset($params['objectType']) && $params['objectType']) 
                $q->where('object_type = ?', $params['objectType']);
                
              if(isset($params['objectId']) && $params['objectId']) 
                $q->where('object_id = ?', $params['objectId']);
                
              // CATERGORY_ID
              if(isset($params['categoryId']) && $params['categoryId']) 
                $q->andWhere('category_id = ?', $params['categoryId']); 

              
              // KEYWORD
              if(isset($params['keyword']) && $params['keyword'])
                $q->andWhere('title LIKE ? OR intro LIKE ? OR content LIKE ?', array('%'.$params['keyword'].'%', '%'.$params['keyword'].'%', '%'.$params['keyword'].'%'));
                
              // IS_FEATURED
              if(isset($params['is_featured']) && $params['is_featured']) $q->andWhere('is_featured = 1');
              
              // SORT
              $params['order'] = (isset($params['order']) && $params['order']) ? $params['order'] : '';
              $q->orderBy($params['order'].' created_at DESC');
              
      $pager = new sfDoctrinePager('News', sfConfig::get('app_pager', 30));
      $pager->setPage($page);
      $pager->setQuery($q);
      $pager->init();
      
      return $pager;
  }


}